﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuickLifeHall.Facade.SystemBase
{
    public class User : Utilities.FacadeBase
    {
        DAL.SystemBase.User repository = new DAL.SystemBase.User();

        #region 页面操作

        [AjaxPro.AjaxMethod]
        public Entity.EasyUI.Grid<Entity.SystemBase.User> GetPageData(string json)
        {
            dynamic param = Core.Util.DeserializeFromJson<dynamic>(json);

            long page = param.page; // Common.HttpUtilities.GetRequestParam<long>(this.Request, "page");
            long rows = param.rows; //Common.HttpUtilities.GetRequestParam<long>(this.Request, "rows");
            string strUserID = param.txtUserID;// Common.HttpUtilities.GetRequestParam<string>(this.Request, "txtUserID");
            string strUserName = param.txtUserName;// Common.HttpUtilities.GetRequestParam<string>(this.Request, "txtUserName");

            PetaPoco.Sql sql = PetaPoco.Sql.Builder.Append("Where 1=1");
            if (!string.IsNullOrEmpty(strUserID))
                sql.Append("AND UserID={0}", strUserID);
            if (!string.IsNullOrEmpty(strUserName))
                sql.Append("AND UserName like '%{0}%'", strUserName);

            Entity.EasyUI.Grid<Entity.SystemBase.User> gridData = new Entity.EasyUI.Grid<Entity.SystemBase.User>();
            var pageData = repository.Page(page, rows, sql);
            gridData.rows = pageData.Items;
            gridData.total = pageData.TotalItems;

            return gridData;
        }

        [AjaxPro.AjaxMethod]
        public Entity.EasyUI.Result EnableUser(int userId, bool enable)
        {
            Entity.EasyUI.Result rst = null;

            try
            {
                Entity.SystemBase.User userEntity = this.repository.Get(userId);
                if (userEntity == null)
                {
                    rst = new Entity.EasyUI.Result("error", "用户不存在！");
                }
                else
                {
                    userEntity.State = enable ? 1 : 0;
                    this.repository.Update(userEntity);
                    rst = new Entity.EasyUI.Result("success", "设置成功");
                }
            }
            catch (Exception ex)
            {
                rst = new Entity.EasyUI.Result("error", "发生错误：" + ex.Message);
            }

            return rst;
        }

        [AjaxPro.AjaxMethod]
        public Entity.EasyUI.Result AddUser(string json)
        {
            Entity.SystemBase.User user = Core.Util.DeserializeFromJson<Entity.SystemBase.User>(json);
            var existsEntity = this.repository.GetByName(user.UserName);
            if (existsEntity != null)
            {
                return new Entity.EasyUI.Result("error", "用户名已存在，添加失败！");
            }
            else
            {
                repository.Insert(user);
                return new Entity.EasyUI.Result("success", "新增用户成功！");
            }
        }

        [AjaxPro.AjaxMethod]
        public Entity.EasyUI.Result DeleteUser(int userId)
        {
            Entity.EasyUI.Result rst = null;

            try
            {
                Entity.SystemBase.User userEntity = this.repository.Get(userId);
                if (userEntity == null)
                {
                    rst = new Entity.EasyUI.Result("error", "用户不存在！");
                }
                else
                {
                    this.repository.Delete(userEntity.UserID);
                    rst = new Entity.EasyUI.Result("success", "删除成功");
                }
            }
            catch (Exception ex)
            {
                rst = new Entity.EasyUI.Result("error", "发生错误：" + ex.Message);
            }

            return rst;
        }

        [AjaxPro.AjaxMethod]
        public Entity.EasyUI.Result EditPassword(string json)
        {
            Entity.EasyUI.Result rst = null;
            Entity.SystemBase.User user = Core.Util.DeserializeFromJson<Entity.SystemBase.User>(json);

            try
            {
                Entity.SystemBase.User userEntity = this.repository.Get(user.UserID);
                if (userEntity == null)
                {
                    rst = new Entity.EasyUI.Result("error", "用户不存在！");
                }
                else
                {
                    userEntity.UserPassword = user.UserPassword;
                    this.repository.Update(userEntity);
                    rst = new Entity.EasyUI.Result("success", "密码修改成功");
                }
            }
            catch (Exception ex)
            {
                rst = new Entity.EasyUI.Result("error", "发生错误：" + ex.Message);
            }

            return rst;
        }

        [AjaxPro.AjaxMethod]
        public Entity.EasyUI.Result EditSelfPassword(string password)
        {
            Entity.EasyUI.Result rst = null;

            try
            {
                Entity.SystemBase.User userEntity = this.repository.Get(this.LoginUser.UserID);
                if (userEntity == null)
                {
                    rst = new Entity.EasyUI.Result("error", "用户不存在！");
                }
                else
                {
                    userEntity.UserPassword = password;
                    this.repository.Update(userEntity);
                    rst = new Entity.EasyUI.Result("success", "密码修改成功");
                }
            }
            catch (Exception ex)
            {
                rst = new Entity.EasyUI.Result("error", "发生错误：" + ex.Message);
            }

            return rst;
        }

        #endregion
    }
}
